<template>
  <div class="article-item_container">
    <!-- 一条文章单元格 -->
    <van-cell border @click="$router.push('/article?art_id='+ obj.art_id)">
      <!-- 标题区域的插槽 -->
      <template #title>
        <div class="title-box">
          <!-- 标题 -->
          <span>{{ obj.title }}</span>
          <!-- 一张图片 -->
          <img
            class="thumb"
            :src="obj.cover.images[0]"
            v-if="obj.cover.type == 1"
          />
        </div>
        <!-- 多张图片 -->
        <div class="thumb-box" v-if="obj.cover.type > 1">
          <img
            class="thumb"
            v-for="(item, index) in obj.cover.images"
            :key="index"
            :src="item"
          />
        </div>
      </template>
      <!-- label 区域的插槽 -->
      <template #label>
        <div class="label-box">
          <div>
            <span>{{ obj.aut_name }}</span>
            <span>{{ obj.comm_count }}评论</span>
            <span>{{ formatTimeAgoFn(obj.pubdate) }}</span>
          </div>
          <!-- 反馈按钮 -->
          <van-icon name="cross" @click.stop="show1 = true" v-if="isShowCross"/>
          <!-- 一级反馈面板 -->
          <van-action-sheet
            v-model="show1"
            :actions="actions1"
            @select="onSelectFn"
            cancel-text="取消"
            get-container="body"
          />
          <!-- 二级反馈面板 -->
          <van-action-sheet
            v-model="show2"
            :actions="actions2"
            @select="onSelectFn"
            cancel-text="返回"
            get-container="body"
          />
          <van-dialog
            v-model="show3"
            title="反馈其他问题"
            show-cancel-button
            get-container="body"
            :before-close="onBeforeCloseFn"
          >
            <van-field
              v-model="msg"
              type="textarea"
              placeholder="请输入反馈的内容1-100字"
              border
              v-foc
            />
          </van-dialog>
        </div>
      </template>
    </van-cell>
  </div>
</template>

<script>
import { FIRST_ACTION_SHEET, SECOND_ACTION_SHEET } from '@/constant'
import { articleDislikeAPI, articleReportsAPI } from '@/api/home'
import { Toast } from 'vant'
export default {
  name: 'ArticleItem',
  props: {
    obj: {
      // 父传子 文章对象
      type: Object,
      required: true
    },
    ind: {
      // 父传子 下标
      type: Number
    },
    isShowCross: {
      type: Boolean,
      default: true
    }

  },
  data () {
    return {
      // 控制一级反馈面板 可见性，false表示隐藏
      show1: false,
      show2: false,
      actions1: FIRST_ACTION_SHEET,
      actions2: SECOND_ACTION_SHEET,
      show3: false,
      msg: ''
    }
  },
  methods: {
    async onSelectFn (actionObj) {
      if (actionObj.id === 22) {
        // 反馈垃圾内容
        // 显示二级反馈面板
        this.show2 = true
      } else if (actionObj.id === 11) {
        // 不感兴趣
        // 删除文章
        this.$emit('del-art', this.ind)
        // 发请求，告诉后台，从数据库中删除
        try {
          await articleDislikeAPI({
            artId: this.obj.art_id
          })
          Toast.success('操作成功')
        } catch (e) {
          Toast.fail('操作失败')
        }
      } else if (actionObj.id === 33) {
        // 拉黑作者
      } else {
        // 二级反馈 - 举报
        if (actionObj.id === 0) {
          this.show3 = true
        } else {
          try {
            await articleReportsAPI({
              artId: this.obj.art_id,
              type: actionObj.id + '',
              remark: actionObj.name
            })
            Toast.success('反馈成功')
          } catch (e) {
            Toast.fail(e.message || '反馈失败')
          }
        }
        // 隐藏 一级、二级反馈面板
        this.show1 = this.show2 = false
      }
    },
    async onBeforeCloseFn (action, done) {
      if (action === 'confirm') {
        if (this.msg.length === 0) {
          Toast('请输入反馈内容')
          // 阻止弹窗关闭
          done(false)
        } else {
          done()
          try {
            await articleReportsAPI({
              artId: this.obj.art_id,
              type: '0',
              remark: this.msg
            })
            Toast.success('反馈成功')
          } catch (e) {
            Toast.fail(e.message || '反馈失败')
          }
        }
      } else {
        done()
      }
      this.msg = ''
    }
  }
}
</script>

<style scoped lang="less">
/* 标题样式 */
.title-box {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
}

/* 图片的样式, 矩形黄金比例：0.618 */
.thumb {
  width: 113px;
  height: 70px;
  background-color: #f8f8f8;
  object-fit: cover;
}

/* 三图, 图片容器 */
.thumb-box {
  display: flex;
  justify-content: space-between;
}

/* label描述样式 */
.label-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

/* 文章信息span */
.label-box span {
  margin: 0 3px;
  &:first-child {
    margin-left: 0;
  }
}
</style>
